package ga.wavaplayer.net {
	import flash.events.StatusEvent;
	import ga.wavaplayer.events.WStreamEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	public class WNetStream extends NetStream{
		
		public var meta_data:Object;
		
		public function WNetStream(connection:NetConnection) {
			super(connection);
			this.addEventListener(NetStatusEvent.NET_STATUS, _netStatusHandler);
			this.addEventListener(StatusEvent.STATUS, _statusHandler);
		}
		
		public function onMetaData(info:Object):void {
			meta_data = info;
			dispatchEvent(new WStreamEvent(WStreamEvent.META_DATA));
		}
		
		public function onCuePoint(info:Object):void {
			dispatchEvent(new WStreamEvent(WStreamEvent.CUE_POINT));
		}
		
		public function onPlayStatus(info:Object):void {
			if (info.code == "NetStream.Play.Complete") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_COMPLETE));
			} else if (info.code == "NetStream.Play.Switch") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_SWITCH));
			}
		}
		
		private function _netStatusHandler(e:NetStatusEvent):void {
			if (e.info.code == "NetStream.Play.Start") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_START));
			}else if (e.info.code == "NetStream.Play.Stop") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_STOP));
			}else if (e.info.code == "NetStream.Play.Failed") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_FAILED));
			}else if (e.info.code == "NetStream.Play.StreamNotFound") {
				dispatchEvent(new WStreamEvent(WStreamEvent.NOT_FOUND));
			}else if (e.info.code == "NetStream.Pause.Notify") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_PAUSE));
			}else if (e.info.code == "NetStream.Unpause.Notify") {
				dispatchEvent(new WStreamEvent(WStreamEvent.PLAYBACK_RESUME));
			}
		}
		
		private function _statusHandler(e:StatusEvent):void {}
	}
}
